﻿
using Microsoft.AspNetCore.Mvc;
using SHZY.Common.Enum;
using SHZY.DependencyInjection;
using SHZY.DynamicApiController;
using SHZY.FriendlyException;
using SHZY.Systems.Entitys.System;
using SqlSugar;

namespace SHZY.BaseData.Common
{
    /// <summary>
    /// 业务实现：WMS系统配置.
    /// </summary>
    [ApiDescriptionSettings(Tag = "BaseData", Name = "ComSysConfig", Order = 200)]
    [Route("api/Common/BaseData/[controller]")]
    public class ComSysConfigService : IDynamicApiController, ITransient
    {
        /// <summary>
        /// 服务基础仓储.
        /// </summary>
        private readonly ISqlSugarRepository<SysConfigEntity> _repository;

        public ComSysConfigService(
        ISqlSugarRepository<SysConfigEntity> sysConfigRepository)
        {
            _repository = sysConfigRepository;
        }

        /// <summary>
        /// 获取指定分类的系统配置.
        /// </summary>
        /// <returns></returns>
        [HttpGet("{category}")]
        public async Task<dynamic> GetList(string category)
        {
            var data = await _repository.AsQueryable().Where(x => x.Category == category).ToListAsync();
            return data;
        }

        /// <summary>
        /// 保存系统配置.
        /// </summary>
        /// <param name="entitys"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task Update([FromBody] List<SysConfigEntity> entitys)
        {
            var isOk = await _repository.UpdateAsync(entitys);
            if (isOk <= 0)
                throw Oops.Oh(ErrorCode.COM1001);
        }
    }
}
